﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XiaLastorm.Sharpshooter
{
	/// <summary>
	/// 用户
	/// </summary>
	[Serializable]
	public class User : IEquatable<User>, IComparable<User>
	{
		private string username = string.Empty;

		/// <summary>
		/// 获取或设置用户名
		/// </summary>
		public string Username
		{
			get { return username; }
			set
			{
				if (value == null)
					username = string.Empty;
				else
					username = value;
			}
		}

		private string nickname = string.Empty;

		/// <summary>
		/// 获取或设置昵称
		/// </summary>
		public string Nickname
		{
			get { return nickname; }
			set
			{
				if (value == null)
					nickname = string.Empty;
				else
					nickname = value;
			}
		}

		private string password = string.Empty;

		/// <summary>
		/// 获取或设置密码
		/// </summary>
		public string Password
		{
			get { return password; }
			set
			{
				if (value == null)
					password = string.Empty;
				else
					password = value;
			}
		}

		/// <summary>
		/// 获取或设置用户信息
		/// </summary>
		public UserInformation Information { get; set; }

		/// <summary>
		/// 合法的用户名字符
		/// </summary>
		public static readonly char[] LegalCharsOfUsername;

		/// <summary>
		/// 初始化 <see cref="User"/> class。
		/// </summary>
		static User()
		{
			LegalCharsOfUsername = GetLegalCharsOfUsername();
		}

		/// <summary>
		/// 生成合法的用户名字符
		/// </summary>
		/// <returns>合法的用户名字符数组</returns>
		private static char[] GetLegalCharsOfUsername()
		{
			List<char> chars = new List<char>();
			for (char ch = char.MinValue; ; ++ch)
			{
				if (!char.IsControl(ch) && !char.IsWhiteSpace(ch))
					chars.Add(ch);
				if (ch == char.MaxValue)
					break;
			}
			return chars.ToArray();
		}

		public static void ValidateUsername(string username)
		{
			if (username == null)
				throw new ArgumentNullException("username");
			if (username == string.Empty)
				throw new ArgumentException("用户名为空");
			if (username.Except(LegalCharsOfUsername).Count() > 0)
				throw new ArgumentException("用户名中含有非法字符");
		}

		/// <summary>
		/// 生成 <see cref="User"/> class 的新实例。
		/// </summary>
		/// <param name="username">用户名</param>
		public User(string username)
		{
			ValidateUsername(username);
			Username = username;
		}

		public bool Equals(User other)
		{
			if (Object.ReferenceEquals(other, null))
				return false;
			if (Object.ReferenceEquals(this, other))
				return true;
			if (this.GetType() != other.GetType())
				return false;
			return Username == other.Username;
		}

		public override bool Equals(object obj)
		{
			return Equals(obj as User);
		}

		public override int GetHashCode()
		{
			return Username.GetHashCode();
		}

		public static bool operator ==(User left, User right)
		{
			if (Object.ReferenceEquals(left, null))
			{
				if (Object.ReferenceEquals(right, null))
					return true;
				return false;
			}
			return left.Equals(right);
		}

		public static bool operator !=(User left, User right)
		{
			return !(left == right);
		}

		public override string ToString()
		{
			return Username;
		}

		public int CompareTo(User other)
		{
			return this.Username.CompareTo(other.Username);
		}
	}
}
